package com.example.myapplication.ui.home;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.BleGATT.ClientUtils;
import com.example.myapplication.R;
import com.example.myapplication.RunActivity;

public class ScanFragment extends Fragment {
        private static final int REQUEST_ENABLE_BT = 1;
        private Button scanButton;
        private RunActivity mActivity;
        private ClientUtils clientUtils;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (RunActivity) requireActivity();
        clientUtils = mActivity.getClientUtils();
    }
        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_scan, container, false);
            scanButton = view.findViewById(R.id.scan_button);

            scanButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BleStart();
                }
            });

            return view;
        }

        public void BleStart() {
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (bluetoothAdapter == null) {
                Toast.makeText(getActivity(), "设备不支持蓝牙功能", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!bluetoothAdapter.isEnabled()) {
                // 如果蓝牙未打开，则请求用户打开蓝牙
                Toast.makeText(mActivity, "蓝牙没有开启，功能无法正常使用！！！", Toast.LENGTH_SHORT).show();
            }
            clientUtils.startScan();


        }

    }
